<?php

namespace App\Http\Controllers\admin\Goods;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class GoodssController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //商品管理
        
        $goods = DB::table('goods')->get();
                // ->join('goodsmain', 'goodsmain.gid', '=', 'goods.id')

        //         // ->get();
        //         dd($goods);
        // dd($goods);
        //  $goodsmain = DB::table('goodsmain')->where('gid',$goods->id)->get();
        foreach($goods as $v)
        {
            $id = $v->id;

            $goodsmain[] = DB::table('goodsmain')->where('gid',$id)->get();
            // dd($goodsmain);
        }
        // dd($goodsmain);
        return view('admin.goods.GoodssIndex',compact('goods','goodsmain'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //添加商品
         $cates=self::CateList();
            // dd($cates);
            foreach ($cates as $k => $v) {
            if($v->cid != 0){
                $res = DB::table('goodscate')->where('id',$v->cid)->first();
                
                $cates[$k]->path= $res->name;
            }else{
                $cates[$k]->path='顶级分类';
            }
        }
        return view('admin.goods.goodssAdd',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //添加存储
        $data = $request->except('_token','_method');

        //执行插入数据
        $row = DB::table('goods')->insert($data);
        if($row){
            return redirect('/admin/goods')->with('success','添加成功');
        }else{
            return back()->with('error','添加成功');

        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //修改页
        $goodsdata = DB::table('goods')->where('id',$id)->first();

         $cates=self::CateList();
            // dd($cates);
            foreach ($cates as $k => $v) {
            if($v->cid != 0){
                $res = DB::table('goodscate')->where('id',$v->cid)->first();
                
                $cates[$k]->path= $res->name;
            }else{
                $cates[$k]->path='顶级分类';
            }
        }
        
        // return view('admin.goods.goodsindex',compact('cates'));
        // dd($goodsdata);
        return view('admin.goods.GoodssEdit',compact('goodsdata','cates'));
    }

     //按照格式查询 分类列表
   static public function CateList()
    {
        $cates = DB::select('select * ,concat(path,",",id) as paths from goodscate order by paths');
        foreach($cates as $k=>$v){
            //把字符串变成数组
            $arr = explode(',',$v->path);
            //获取长度
            $len = count($arr)-1;
            $cates[$k]->name=str_repeat('|-------',$len).$v->name;
        }

        return $cates;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //提交修改
         $data = $request->except('_token','_method');
// dd($data['id']);
        //执行插入数据
        $row = DB::table('goods')->where('id',$data['id'])->update($data);
        if($row){
            return redirect('/admin/goods')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');

        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $res = DB::table('goodsmain')->where('gid',$id)->first();
        // dd($res);
        if($res){
            return redirect('/admin/goods')->with('error','删除失败，有商品颜色分类');
        }else{
            $row = DB::table('goods')->where('id',$id)->delete();
            if($row){
                return redirect('/admin/goods')->with('success','删除成功');
            }else{
                return redirect('/admin/goods')->with('error','删除失败');
            }
        }
        
    }



}
